package ca.etsmtl.gti785.util;

/**
 * This static class calculates the distance between two latitudes and longitudes points.
 * Source: http://stackoverflow.com/a/215849
 * @author Etienne Faucher
 */
public class DistanceCalculator {
	private static final double Pi = 3.141592653589793;
    private static final double Radius = 6378.16;
    
    private DistanceCalculator() 
    { 
    }
    
    public static double Radians(double x)
    {
        return x * Pi / 180;
    }
    
    public static double DistanceBetweenPlaces(double longitude1, double latitude1, double longitude2, double latitude2)
    {
        double dlon = Radians(longitude2 - longitude1);
        double dlat = Radians(latitude2 - latitude1);

        double a = (Math.sin(dlat / 2) * Math.sin(dlat / 2)) + Math.cos(Radians(latitude1)) * Math.cos(Radians(latitude2)) * (Math.sin(dlon / 2) * Math.sin(dlon / 2));
        double angle = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        return angle * Radius;
    }
}
